import { _decorator, Component, Node, Label, v3, tween, UITransform, Sprite, Color } from 'cc';
const { ccclass, property } = _decorator;

import { ShuZiHuaRongDao_GameData } from "./ShuZiHuaRongDao_GameData";
import { ShuZiHuaRongDao_GameManager } from "./ShuZiHuaRongDao_GameManager";

@ccclass('ShuZiHuaRongDaoBlock')
export default class ShuZiHuaRongDao_Block extends Component {
    @property(Node)
    normal: Node | null = null;
    @property(Node)
    bg: Node | null = null;
    @property(Label)
    Label_id: Label | null = null;
    col: number = 0;
    row: number = 0;
    length: number = 1;
    id: number; //从1开始的数字编号
    //    // 只序列化需要的属性
    toJSON() {
        return {
            col: this.col,
            row: this.row,
            id: this.id,
            // _class: 'MyClass' // 添加类标识以便反序列化
        };
    }
    //    // 初始化
    init(_col: number, _row: number, _id: number) {
        this.col = _col;
        this.row = _row;
        this.id = _id;

        this.node.active = true;
        this.InitWidthAndHeight(ShuZiHuaRongDao_GameData.size, ShuZiHuaRongDao_GameData.size);
        this.InitPosition();

        this.Refresh();

        this.node.on(Node.EventType.TOUCH_END, () => {
            ShuZiHuaRongDao_GameManager.Instance.onBlockClick(this);
        });
    }
    async Refresh() {
        this.Refresh_number();
        // this.Refresh_color();
    }
    async Refresh_number() {
        if (this.id === 0) {
            this.Label_id.string = "";
            this.normal.active = false; // 隐藏背景或块体节点
        } else {
            this.Label_id.string = `${this.id}`;
            this.normal.active = true;
        }
    }
    async Refresh_color() {
        if (this.id !== 0) {
            const index = this.id - 1; // 注意 id 从 1 开始，所以减 1
            const row = Math.floor(index / ShuZiHuaRongDao_GameData.col); // 从上往下第几行（从 1 开始）
            // console.log(`row`, row);

            let item_data = ShuZiHuaRongDao_GameData.colors[row];
            let color = item_data.color;
            this.bg.getComponent(Sprite).color = new Color().fromHEX(color);

            let Label_color = item_data.Label_color;
            this.Label_id.node.getComponent(Sprite).color = new Color().fromHEX(Label_color);
        }
    }
    //    // 初始化单元宽高
    InitWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    InitPosition() {
        let x = -ShuZiHuaRongDao_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        let y = ShuZiHuaRongDao_GameData.H / 2 - this.node.getComponent(UITransform).height / 2 - this.node.getComponent(UITransform).height * (this.row - 1);
        this.node.position = v3(x, y, 0);
    }
    MovePosition(_duration = 0.1) {
        return new Promise<void>(resolve => {
            let x = -ShuZiHuaRongDao_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
            let y = ShuZiHuaRongDao_GameData.H / 2 - this.node.getComponent(UITransform).height / 2 - this.node.getComponent(UITransform).height * (this.row - 1);
            tween(this.node)
                .to(_duration, { position: v3(x, y, 0) })
                .call(() => resolve())
                .start();
        });
    }
}